<template>
  <basic-container>
    <avue-form :option="option" v-model="form">
      <!-- <template #footer>
        <span class="dialog-footer">
          <el-button type="primary" @click="handleDoDeploy" :loading="deployLoading"
            >提 交</el-button
          >
        </span>
      </template> -->
      <template #fileUp="{ row }">
        <el-upload
          class="upload-demo"
          drag
          :action="url()"
          multiple
          :headers="headers"
          :data="{ ...this.form.metaLabel }"
        >
          <el-icon class="el-icon--upload"><upload-filled /></el-icon>
          <div class="el-upload__text">将文件拖拽到此处，或<em>点击上传</em></div>
          <template #tip>
            <div class="el-upload__tip">
              只能上传pdf/doc/txt/md/csv/html/docx/zip格式文件，且不超过128MB
            </div>
          </template>
        </el-upload>
      </template>
    </avue-form>
  </basic-container>
</template>
<script>
import { UploadFilled } from '@element-plus/icons-vue';
import { getList, getListByDB } from '@/api/knowledge/datasetList';
import { baseUrl } from '@/config/env';
import { getToken } from '@/utils/auth';
export default {
  data() {
    return {
      headers: {
        'Blade-Auth': 'bearer ' + getToken(),
        'Blade-Requested-With': 'BladeHttpRequest',
        // Authorization: getToken(),
      },
      form: {},
      option: {
        // url: `https://cagpt.ngrok.io/blade-api/chat/knowledge-base/${this.form.dataList}/document`,
        menuBtn: false,
        column: [
          {
            label: '数据集',
            prop: 'dataList',
            type: 'radio',
            dicData: [],
            // row: true,
            span: 24,
            props: {
              label: 'name',
              value: 'kbId',
            },
            change: val => {
              console.log(val, 'val');
              if (val && val.item) {
                this.metaLabelData(val.item);
              }
              // this.metaLabelData(val.item);
              // this.form.metaLabel = `{\"${val.item[0].metaLabel}\":{\"value\":\"${val.item[0].name}\"}}`;
              // this.form.metaLabel = JSON.stringify(this.form.metaLabel);
              // this.form.metaLabel = { doc_metadata: this.form.metaLabel };
            },
            // value: '04850c8d-088d-49e2-81a8-f741d48870ce',
          },
          {
            label: '文档上传',
            prop: 'fileUp',
            type: 'radio',
            span: 18,
            slot: true,
          },
        ],
      },
    };
  },
  mounted() {
    getListByDB(1, 100, '').then(res => {
      const dataList = this.findObject(this.option.column, 'dataList');
      console.log(res.data.records, 'res');
      if (res && res.data && res.data.records && res.data.records.length > 0) {
        const data = res.data.records;
        dataList.dicData = data;
        this.metaLabelData(data[0]);
        this.form.dataList = data[0].kbId;
        // this.form.metaLabel = `{\"${data[0].metaLabel}\":{\"value\":\"${data[0].name}\"}}`;
        // this.form.metaLabel = JSON.stringify(this.form.metaLabel);
        // this.form.metaLabel = { doc_metadata: this.form.metaLabel };
      } else {
        dataList.dicData = [];
        this.form.dataList = '';
      }
    });
  },
  methods: {
    url() {
      const url =
        import.meta.env.VITE_APP_API +
        `/blade-api/chat/knowledge-base/${this.form.dataList}/document`;
      return url;
    },
    metaLabelData(val) {
      console.log(val, 'val');
      this.form.metaLabel = `{\"${val.metaLabel}\":{\"value\":\"${val.name}\"}}`;
      this.form.metaLabel = JSON.stringify(this.form.metaLabel);
      this.form.metaLabel = { doc_metadata: this.form.metaLabel };
    },
  },
};
</script>